package top.lanscarlos.ashcraft.controller;

import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.lanscarlos.ashcraft.service.CartService;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.controller
 *
 * @author Lanscarlos
 * @since 2022-05-26 21:36
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class CartController extends AshController {
    @Autowired
    private CartService service;

    @RequestMapping("/instance")
    public Object query(int userId) {
        return service.query(userId);
    }


//    @RequestMapping("/modify")
    @Deprecated
    public Object modify(
            int cartId,
            int commodityId,
            @NotNull String operation,
            int amount
    ) {
        return toJson(service.modify(cartId, commodityId, operation, amount));
    }

    @RequestMapping("/select")
    public Object select(
            int cartId,
            int itemId,
            boolean selected
    ) {
        return toJson(service.select(cartId, itemId, selected));
    }

    @RequestMapping("/add")
    public Object add(
            int cartId,
            int commodityId
    ) {
        return toJson(service.addItem(cartId, commodityId));
    }

    @RequestMapping("/remove")
    public Object remove(
            int cartId,
            int itemId
    ) {
        return toJson(service.removeItem(cartId, itemId));
    }

    @RequestMapping("/increase")
    public Object increase(
            int cartId,
            int itemId
    ) {
        return toJson(service.increaseAmount(cartId, itemId));
    }

    @RequestMapping("/decrease")
    public Object decrease(
            int cartId,
            int itemId
    ) {
        return toJson(service.decreaseAmount(cartId, itemId));
    }

}
